﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataInterpolator.Model;
using GalaSoft.MvvmLight;
using DataInterpolator.Properties;
using DataInterpolator.ViewModel.Messages;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Win32;

namespace DataInterpolator.ViewModel
{
   public class ExportInterpolationViewModel : ViewModelBase
   {
      private readonly IDataService _DataService;
      private IList<DataSet> _InterpolatedSets = new List<DataSet>();

      public ExportInterpolationViewModel(IDataService dataService)
      {
         _DataService = dataService;

         Messenger.Default.Register<ExportInterpolationMessage>(this, OnFullRefresh);
      }

      private void OnFullRefresh(ExportInterpolationMessage message)
      {

         _DataService.GetInterpolatedValues((left, interp, right, leftTrueOffset, rightTrueOffset) =>
         {
            _InterpolatedSets = interp;



         });


      }

      /// <summary>
      /// Sets and gets the Filename property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string Filename
      {
         get
         {
            return Settings.Default.Export_Filename;
         }

         set
         {
            if (Settings.Default.Export_Filename == value)
            {
               return;
            }

            Settings.Default.Export_Filename = value;
            RaisePropertyChanged("Filename");
         }
      }


      /// <summary>
      /// Sets and gets the Company property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string Company
      {
         get
         {
            return Settings.Default.Export_Company;
         }

         set
         {
            if (Settings.Default.Export_Company == value)
            {
               return;
            }

            Settings.Default.Export_Company = value;
            RaisePropertyChanged("Company");
         }
      }

      /// <summary>
      /// Sets and gets the WellTitle property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string WellTitle
      {
         get
         {
            return Settings.Default.Export_WellTitle;
         }

         set
         {
            if (Settings.Default.Export_WellTitle == value)
            {
               return;
            }

            Settings.Default.Export_WellTitle = value;
            RaisePropertyChanged("WellTitle");
         }
      }

      /// <summary>
      /// Sets and gets the Field property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string Field
      {
         get
         {
            return Settings.Default.Export_Field;
         }

         set
         {
            if (Settings.Default.Export_Field == value)
            {
               return;
            }

            Settings.Default.Export_Field = value;
            RaisePropertyChanged("Field");
         }
      }

      /// <summary>
      /// Sets and gets the Location property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string Location
      {
         get
         {
            return Settings.Default.Export_Location;
         }

         set
         {
            if (Settings.Default.Export_Location == value)
            {
               return;
            }

            Settings.Default.Export_Location = value;
            RaisePropertyChanged("Location");
         }
      }

      /// <summary>
      /// Sets and gets the API property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string API
      {
         get
         {
            return Settings.Default.Export_API;
         }

         set
         {
            if (Settings.Default.Export_API == value)
            {
               return;
            }

            Settings.Default.Export_API = value;
            RaisePropertyChanged("API");
         }
      }

      /// <summary>
      /// Sets and gets the Country property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string Country
      {
         get
         {
            return Settings.Default.Export_Country;
         }

         set
         {
            if (Settings.Default.Export_Country == value)
            {
               return;
            }

            Settings.Default.Export_Country = value;
            RaisePropertyChanged("Country");
         }
      }

      /// <summary>
      /// Sets and gets the County property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string County
      {
         get
         {
            return Settings.Default.Export_County;
         }

         set
         {
            if (Settings.Default.Export_County == value)
            {
               return;
            }

            Settings.Default.Export_County = value;
            RaisePropertyChanged("County");
         }
      }

      /// <summary>
      /// Sets and gets the State property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string State
      {
         get
         {
            return Settings.Default.Export_State;
         }

         set
         {
            if (Settings.Default.Export_State == value)
            {
               return;
            }

            Settings.Default.Export_State = value;
            RaisePropertyChanged("State");
         }
      }

      /// <summary>
      /// Sets and gets the ServiceCompany property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public string ServiceCompany
      {
         get
         {
            return Settings.Default.Export_ServiceCompany;
         }

         set
         {
            if (Settings.Default.Export_ServiceCompany == value)
            {
               return;
            }

            Settings.Default.Export_ServiceCompany = value;
            RaisePropertyChanged("ServiceCompany");
         }
      }

      /// <summary>
      /// Sets and gets the LogDate property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public DateTime LogDate
      {
         get
         {
            return Settings.Default.Export_LogDate;
         }

         set
         {
            if (Settings.Default.Export_LogDate == value)
            {
               return;
            }

            Settings.Default.Export_LogDate = value;
            RaisePropertyChanged("LogDate");
         }
      }

      private double _StartDepth = 0.0;

      /// <summary>
      /// Sets and gets the StartDepth property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public double StartDepth
      {
         get
         {
            return _StartDepth;
         }

         set
         {
            if (_StartDepth == value)
            {
               return;
            }

            _StartDepth = value;
            RaisePropertyChanged("StartDepth");
         }
      }

      private double _StopDepth = 0.0;

      /// <summary>
      /// Sets and gets the StopDepth property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public double StopDepth
      {
         get
         {
            return _StopDepth;
         }

         set
         {
            if (_StopDepth == value)
            {
               return;
            }

            _StopDepth = value;
            RaisePropertyChanged("StopDepth");
         }
      }

      private double _Step = 0.0;

      /// <summary>
      /// Sets and gets the Step property.
      /// Changes to that property's value raise the PropertyChanged event. 
      /// </summary>
      public double Step
      {
         get
         {
            return _Step;
         }

         set
         {
            if (_Step == value)
            {
               return;
            }

            _Step = value;
            RaisePropertyChanged("Step");
         }
      }

      private RelayCommand _BrowseCommand;

      /// <summary>
      /// Gets the BrowseCommand.
      /// </summary>
      public RelayCommand BrowseCommand
      {
         get
         {
            return _BrowseCommand
                ?? (_BrowseCommand = new RelayCommand(
                () =>
                {
                   SaveFileDialog sfd = new SaveFileDialog();
                   sfd.AddExtension = true;
                   sfd.DefaultExt = ".las";
                   sfd.Filter = "LAS File (*.las)|*.las|All Files (*.*)|*.*";
                   sfd.Title = "Specify the LAS File Name";

                   if (sfd.ShowDialog().GetValueOrDefault(false))
                   {
                      Filename = sfd.FileName;
                   }
                }));
         }
      }

      private RelayCommand _ExportCommand;

      /// <summary>
      /// Gets the ExportCommand.
      /// </summary>
      public RelayCommand ExportCommand
      {
         get
         {
            return _ExportCommand ?? (_ExportCommand = new RelayCommand(() =>
            {
               ExportInterpInfo info = new ExportInterpInfo()
               {
                  API = API,
                  Company = Company,
                  Country = Country,
                  County = County,
                  Field = Field,
                  Filename = Filename,
                  Location = Location,
                  LogDate = LogDate,
                  ServiceCompany = ServiceCompany,
                  State = State,
                  WellTitle = WellTitle
               };

               _DataService.ExportInterpolation(info);
            }));
         }
      }
   }
}
